var tuoshuo_magage = {
	init:function()
	{
		
		var $start_datepicker = $("#start_datepicker");
		var $end_datepicker = $("#end_datepicker");
		if($start_datepicker.length > 0){
			$start_datepicker.datepicker();
		}
		if($end_datepicker.length > 0){
			$end_datepicker.datepicker();
		}	
		
		//bind form
		var $member_submit = $("#admin_news_form");
		if($member_submit.length > 0){
		  $member_submit.submit(function(){
			 return tuoshuo_magage.checkForm();
		   });
		}
		
		var $cancelbtn = $('.info-list-box').find('.cancel-status');
		
		$cancelbtn.each(function(index, element) {
            $(this).bind('click',function(){
				tuoshuo_magage.canceldo($(this));
			});
        });
		
		
		var $initiatelbtn = $('.info-list-box').find('.initiate-status');
		
		$initiatelbtn.each(function(index, element) {
            $(this).bind('click',function(){
				tuoshuo_magage.initiatedo($(this));
			});
        });
		
		
		var $cancelHotbtn = $('.info-list-box').find('.cancel-hot');
		
		$cancelHotbtn.each(function(index, element) {
            $(this).bind('click',function(){
				tuoshuo_magage.cancelHotdo($(this));
			});
        });
		
		var $initiatelHotbtn = $('.info-list-box').find('.initiate-hot');
		
		$initiatelHotbtn.each(function(index, element) {
            $(this).bind('click',function(){
				tuoshuo_magage.initiateHotdo($(this));
			});
        });
 
					
	} ,
	checkForm:function(){
		
		
		var $start_datepicker = $("#start_datepicker");
		var $end_datepicker = $("#end_datepicker");
		var join_start_time = '';
		var join_end_time = '';
		
		join_start_time = $start_datepicker.val();
		join_end_time = $end_datepicker.val();
		
		if(join_start_time && !join_end_time){
			 var $date = new Date();
			 var strNowDate = "" + $date.getFullYear() + "-";
			     strNowDate += ($date.getMonth()+1) + "-";
			     strNowDate += $date.getDate() ;
			 $end_datepicker.val(strNowDate);
			 join_end_time = strNowDate;
		}
		
		if(join_start_time > join_end_time){
			alert('开始时间不能大于结束时间');
			return false;
		}
		
		var nullNum = 0;
		var $member_submit = $("#admin_news_form");
		var $inputs = $member_submit.find("input[type='text']");
		var $selects = $member_submit.find("select");
		
		if($inputs.length > 0){
			$inputs.each(function(i, e) {
				var ival=$(this).val();
				var data = $(this).data('name');
				ival = $.trim(ival);
				$(this).removeAttr('name')
				if(ival!==""){
					nullNum +=1;
					$(this).attr('name',data);
				}
			});
		}
		
		if($selects.length > 0){
			$selects.each(function(i, e) {
				var data = $(this).data('name');
				var sval=$(this).val();
				sval = $.trim(sval);
				$(this).removeAttr('name')
				if(sval!==""){
					nullNum +=1;
					$(this).attr('name',data)
				}
			});
		}
		
		
		if(nullNum === 0){
			return false;
		}else{
			return true;
		}

	},
	canceldo:function($self){
		var id = $self.data('id');
		if(id){
			$self.html('<img src="http://www.gzcanr.com/images/public/loading.gif" >');
			$self.attr('disabled',"true");
			$.get("/admins/tuoshuo_magage.html",{ "id": id, "cmd": "dostate","type":"nopass" } ,function(result){
                if(result=='1'){
					alert('操作成功');
					document.location.reload();
				}else{
					$self.html('下架');
					$self.attr('disabled',"false");
					alert('操作失败');
				}
            });
		}
	},
	initiatedo:function($self){
		var id = $self.data('id');
		if(id){
			$self.html('<img src="http://www.gzcanr.com/images/public/loading.gif" >');
			$self.attr('disabled',"true");			
			$.get("/admins/tuoshuo_magage.html",{ "id": id, "cmd": "dostate","type":"pass" } ,function(result){
                if(result=='1'){
					alert('操作成功');
					document.location.reload();
				}else{
					$self.html('上架');
					$self.attr('disabled',"false");
					alert('操作失败');
				}
            });
		}
	} ,
	cancelHotdo:function($self){
		var id = $self.data('id');
		if(id){
			$self.html('<img src="http://www.gzcanr.com/images/public/loading.gif" >');
			$self.attr('disabled',"true");
			$.get("/admins/tuoshuo_magage.html",{ "id": id, "cmd": "dohot","type":"nohot" } ,function(result){
                if(result=='1'){
					alert('操作成功');
					document.location.reload();
				}else{
				    $self.html('取消热门');
					$self.attr('disabled',"false");
					alert('操作失败');
				}
            });
		}
	},
	initiateHotdo:function($self){
		var id = $self.data('id');
		if(id){
			$self.html('<img src="http://www.gzcanr.com/images/public/loading.gif" >');
			$self.attr('disabled',"true");
			$.get("/admins/tuoshuo_magage.html",{ "id": id, "cmd": "dohot","type":"hot" } ,function(result){
                if(result=='1'){
					alert('操作成功');
					document.location.reload();
				}else{
				    $self.html('热门');
					$self.attr('disabled',"false");
					alert('操作失败');
				}
            });
		}
	} 
}


$(function(){
    tuoshuo_magage.init();
});